package com.sinosoft.vaccinetoai.server.business.chatchainrecord.converter;

import com.sinosoft.vaccinetoai.common.CommonConstants;
import com.sinosoft.vaccinetoai.dao.po.business.ChatChainRecord;
import com.sinosoft.vaccinetoai.security.utils.SecurityUtils;
import com.sinosoft.vaccinetoai.server.business.chatchainrecord.addchatchainrecord.io.AddChatChainRecordIO;
import com.sinosoft.vaccinetoai.utils.IdUtils;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author xiazhihao
 * @ClassName: ChatChainRecordConverter
 * @ClassNameExplain:
 * @Description:
 * @date 2023/12/18 22:36
 */
public class ChatChainRecordConverter {

    private ChatChainRecordConverter(){

    }

    public static ChatChainRecord toChatChainRecord(AddChatChainRecordIO io){

        if (Objects.isNull(io)){
            return null;
        }

        ChatChainRecord chatChainRecord = new ChatChainRecord();
        chatChainRecord.setId(IdUtils.generateId(CommonConstants.ID_GEN_KEY_COMMON));
        chatChainRecord.setChat(io.getChat());
        chatChainRecord.setFile(io.getFile());
        //设置用户id
        chatChainRecord.setUserId(SecurityUtils.getPrincipal().getUserId());
        chatChainRecord.setType(io.getType());
        chatChainRecord.setCreateTime(new Date());
        chatChainRecord.setUpdateTime(new Date());
        return chatChainRecord;
    }

    public static List<ChatChainRecord> toChatChainRecords(List<AddChatChainRecordIO> addChatChainRecordIOS){
        if (CollectionUtils.isEmpty(addChatChainRecordIOS)){
            return Collections.emptyList();
        }
        return addChatChainRecordIOS.stream().map(ChatChainRecordConverter::toChatChainRecord).collect(Collectors.toList());
    }

}
